﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Kyoh.App.Renamer
{
    public partial class PreferencesForm : Form
    {
        private static Properties.Settings Conf { get { return Properties.Settings.Default; } }

        public PreferencesForm()
        {
            InitializeComponent();
            defaultRenameTableLayoutPanel.Parent = preferencePanel;
            dialogTableLayoutPanel.Parent = preferencePanel;
            preferencesMenuTreeView.Nodes[0].Tag = defaultRenameTableLayoutPanel;
            preferencesMenuTreeView.Nodes[1].Tag = dialogTableLayoutPanel;
            tabControl1.Visible = false;
        }

        protected override void OnLoad(EventArgs e)
        {
            LoadPreferences();
            base.OnLoad(e);
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (this.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                SavePreferences();
                Conf.Save();
            }
            else
                Conf.Reload();
            base.OnFormClosing(e);
        }

        private void LoadPreferences()
        {
            comboBoxMethod.SelectedValue = Conf.DefaultRenameMethod;
            checkBoxExcept.Checked = Conf.DefaultExceptFromList;
            checkBoxSearchIgnoreCase.Checked = Conf.DefaultIgnoreCase;

            comboBoxNumberStyle.SelectedValue = Conf.DefaultSerialNumberStyle;
            checkBoxForEachDirectory.Checked = Conf.DefaultSerialForEachDirectory;
            numericUpDownSerialStart.Value = Conf.DefaultSerialStart;
            numericUpDownSerialStep.Value = Conf.DefaultSerialStep;

            removeRenameDoNotShowCheckBox.Checked = Conf.ConfirmRemovingAgain != System.Windows.Forms.DialogResult.None;
            clearRenameDoNotShowCheckBox.Checked = Conf.ConfirmClearingAgain != System.Windows.Forms.DialogResult.None;
        }

        private void SavePreferences()
        {
            Conf.DefaultRenameMethod = (PatternMethod)comboBoxMethod.SelectedValue;
            Conf.DefaultExceptFromList = checkBoxExcept.Checked;
            Conf.DefaultIgnoreCase = checkBoxSearchIgnoreCase.Checked;

            Conf.DefaultSerialNumberStyle = (NumberStyle)comboBoxNumberStyle.SelectedValue;
            Conf.DefaultSerialForEachDirectory = checkBoxForEachDirectory.Checked;
            Conf.DefaultSerialStart = numericUpDownSerialStart.Value;
            Conf.DefaultSerialStep = numericUpDownSerialStep.Value;

            Conf.ConfirmRemovingAgain = removeRenameDoNotShowCheckBox.Checked ? DialogResult.OK : DialogResult.None;
            Conf.ConfirmClearingAgain = clearRenameDoNotShowCheckBox.Checked ? DialogResult.OK : DialogResult.None;
        }

        private void preferencesMenuTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            Control selectPanel;
            if (e.Node != null
                && (selectPanel = e.Node.Tag as Control) != null)
            {
                selectPanel.Visible = true;
            }
        }

        private void preferencesMenuTreeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            Control unselectPanel;
            if (preferencesMenuTreeView.SelectedNode != null
                && (unselectPanel = preferencesMenuTreeView.SelectedNode.Tag as Control) != null)
            {
                unselectPanel.Visible = false;
            }
        }
    }
}